var moneyList = ""; //充值金额数据加载
var indexNumber = ""; //选中的充值金额数据下标
var canpay=true;
Page({
  data: {
    code: '获取',
    indexNumber: 0,
    showit: false,
    balance: '0.00',
    name: '-1',
    idcard: '-1',
    phone: '-1',
    info: {},
    datasList: [],
    kefushow: false,
  },
  closePopUp: function() {
    var that = this;
    that.setData({
      kefushow: false
    })
  },

  getcode: function() {
    var vm = this
    this.setData({
      code: 9
    })
    var codeinter = setInterval(function() {
      if (vm.data.code <= 1) {
        clearInterval(codeinter)
        vm.setData({
          code: "获取"
        })
      } else {
        vm.setData({
          code: vm.data.code - 1
        })
      }
    }, 1000)
  },
  namechange: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  idcardchange: function(e) {
    this.setData({
      idcard: e.detail.value
    })
  },

  phonechange: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  commit: function() {
    if (this.data.name == "-1") {
      this.setData({
        name: ''
      })
      return
    }
    if (this.data.idcard == "-1") {
      this.setData({
        idcard: ''
      })
      return
    }
    if (this.data.phone == "-1") {
      this.setData({
        phone: ''
      })
      return
    }
    console.log(this.data.name)
  },
  close: function() {
    this.setData({
      showit: false,
    })
  },
  dealDetail: function() {
    wx.navigateTo({
      url: "../../transaction_record/transaction_record",
      success: function() {},
      fail: function() {}
    })
  },

  detail: function() {
    wx.navigateTo({
      url: "../mydetail/mydetail",
      success: function() {},
      fail: function() {}
    })
  },

  brokerageDetail: function() {
    wx.navigateTo({
      url: "/pages/commission_home/classify",
      success: function() {},
      fail: function() {}
    })
  },
  getbalance: function() {
    var vm = this
    var data = {}
    var userIds = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
    getApp().requestLoading(getApp().globalData.https + "/orderResource/walletBalance/" + userIds,
      "get", data, "加载中..",
      function(res) {
        if (res.code == 0) {
          vm.setData({
            info: res.data,
            balance: res.data.balance,
          })
        }
      },
      function(res) {});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      moneyList: that.data.datasList
    })
    var vm = this
    var data = {}
    this.getbalance()

    getApp().request(getApp().globalData.https + "/orderResource/exchangeRate",
      "get", data,
      function(res) {
        if (res.code == 0) {
          var data = res.data.shift();
          console.log(JSON.stringify(data))
          vm.setData({
            moneyList: res.data
          })
        }
      },
      function(res) {});
  },

  clickCommentText: function(e) {
    var that = this;
    indexNumber = e.currentTarget.dataset.number;
    that.setData({
      indexNumber: indexNumber
    })
  },

  //长按支付后的图片弹出保存图片
  viewImage(e) {
    const {
      src
    } = e.target.dataset;
    wx.previewImage({
      urls: [src],
    });
  },
  /**
   * 立即支付按钮触发
   */
  nowPay: function(e) {
    if (!canpay) {
      return
    }
    canpay = !canpay;
    var vm = this
    let formId = e.detail.formId;
    console.log('form发生了submit事件，推送码为：', formId);
    indexNumber = this.data.indexNumber //获取选中的数据下标
    console.log(indexNumber)
    if (indexNumber == undefined) {
      wx.showToast({
        title: '请选择您要充值的金额',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    moneyList = this.data.moneyList; //获取加载的数据列表
    var id = moneyList[indexNumber].id //得到选中的id
    var cnyCoin = moneyList[indexNumber].cnyCoin //得到充值金额 
    console.log(cnyCoin)
    var datas = {
      "body": "天天必听听币充值",
      "totalPay": cnyCoin,
      "type": "0",
      "userId": wx.getStorageSync("userId")
    }
    getApp().requestLoading(getApp().globalData.https + "orderResource/order",
      "post", datas, "加载中...",
      function(res) {
        console.log(JSON.stringify(res))
        var r = res.data
        if (res.code == 0) {
          wx.requestPayment({
            'timeStamp': r.timeStamp,
            'nonceStr': r.nonceStr,
            'package': "prepay_id=" + r.package,
            'signType': 'MD5',
            'paySign': r.paySign,
            'success': function(res) {
              
              getApp().request(getApp().globalData.https + "resourcesResource/payWaiterWXPic?type=0",
                "get", '',
                function(res) {
                  canpay = !canpay;
                  vm.setData({
                    kefubg: getApp().globalData.source + "dialog/kefu.png", //需要调用接口获取二维码TODO....
                    kefuerweim: res.data.location,
                    kefushow: true, //打开支付成功弹框
                  })
                }
              )

              vm.getbalance()
              var parm = {
                "formId": formId,
                "mark": "通知",
                "name": "充值成功",
                "pageIndex": "pages/style/style",
                "amount": cnyCoin,
                "userId": wx.getStorageSync("userId")
              }
              getApp().request(getApp().globalData.https + "customer/recharge/notice",
                "post", parm,
                function(res) {}
              )



            },
            'fail': function(res) {
              canpay = !canpay;
              wx.showToast({
                title: '支付失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      });


  }
})